package com.hc.javabasestudyproject.juc.fastsafefail;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: huangcheng
 * @CreateTime: 2024-04-10  16:53
 * @Description:
 * @Version: 1.0
 */
public class SafeFailTest {
    public static void main(String[] args){
        Map<String,String> map=new ConcurrentHashMap<>(3);
        map.put("A","Java");
        map.put("B","Python");
        map.put("C","C++");
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry<String, String> entry = iterator.next();
            System.out.println(entry.getKey()+": "+entry.getValue());
            // 模拟在迭代过程中修改集合结构
            if(entry.getKey().equals("A")){
                // 直接修改集合会抛出异常导致fail-safe
                map.remove("B");
            }
        }
    }
}
